﻿using WebApp.BLL;
using WebApp.Web.Core.Auth;
using WebApp.BLL.Manage;
using WebApp.Common;
using WebApp.Models;
using WebApp.ViewModels;
using WebApp.ViewModels.Manage;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebApp.Web.Core;

namespace kzjd.rycp.WebApp.Areas.Manage.Controllers
{
    public class GridcolumnController : Controller
    {

        private static readonly log4net.ILog log = log4net.LogManager.GetLogger("WebApp.Log");

        #region List

        // GET: Manage/Role
        [AuthorizeFilter("Manage.GridColumn")]
        public ActionResult List()
        {
            //编辑权限
            ViewBag.CanEdit = UserAuthBLL.HasAuth("Manage.LangRes.Edit");

            //获取页面状态
            var pageState = Session["Manage.GridColumn.PageState"] as PageStateModel;
            if (pageState == null)
                pageState = new PageStateModel
                {
                    page = 1,
                    rows = 20,
                    sort = "Id",
                    order = "asc"
                };

            //页面状态
            ViewBag.PageState = pageState;

            //查询条件
            ViewBag.Searchkey = Utils.NvStr(pageState.queryParams["s"]);
            ViewBag.Pagekey = Utils.NvStr(pageState.queryParams["pagekey"]);

            //资源分类
            ViewBag.PagekeyList = new SelectList(new S_gridcolumnService().DistinctPagekey());

            return View();
        }

        [AuthorizeFilter("Manage.GridColumn")]
        public JsonResult GridcolumnListJson(int page, int rows, string s, string pagekey, string sort, string order)
        {

            #region 保存页面状态

            PageStateModel pageState = new PageStateModel
            {
                page = page,
                rows = rows,
                sort = sort,
                order = order
            };
            pageState.queryParams["pagekey"] = pagekey;
            pageState.queryParams["s"] = s;

            Session.Add("Manage.GridColumn.PageState", pageState);
            #endregion

            S_gridcolumnService columnservice = new S_gridcolumnService();
            S_userService userservice = new S_userService();

            int rowscount = 0;

            var list = columnservice.PageList(page, rows, s, pagekey, 0, sort, order, out rowscount).Select(p => new
            {
                p.Id,
                p.Sort,
                p.Pagekey,
                p.Field,
                p.Title,
                p.Title_en,
                p.TitleReskey,
                p.Align,
                p.Width,
                p.Sortable,
                p.Islist,
                p.Isexport,
                Updatetime = CommonStr.GetDateTimeStr(p.Updatetime)
            });

            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("total", rowscount);
            result.Add("rows", list);

            return Json(result);
        }

        #endregion

        #region 删除

        [AuthorizeFilter("Manage.GridColumn")]
        public JsonResult Delete(string ids)
        {
            try
            {
                new S_gridcolumnService().DeleteByIds(ids);

                return Json(new AjaxResult("0", CSMSLang.Get("message_delete_success")));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return Json(new AjaxResult(AjaxResult.RESULT_ERROR, ex.Message));
            }
        }

        #endregion

        #region 保存

        [AuthorizeFilter("Manage.GridColumn")]
        public JsonResult Save(string jsondata)
        {
            try
            {
                var service = new S_gridcolumnService();

                //序列化Json数据
                JsonDataModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonDataModel>(jsondata);

                S_gridcolumn item = new S_gridcolumn();

                //如果是修改数据
                if (model.Id > 0)
                {
                    item = service.Find(model.Id);
                }
                //将Json数据赋值给Model对象
                model.SetModelData<S_gridcolumn>(item);

                if (service.Exist(item.Id, item.Pagekey, item.Field, item.Userid))
                {
                    return Json(new AjaxResult(AjaxResult.RESULT_ERROR, "已经存在相同的字段名！"));
                }

                //保存
                service.Save(item);

                return Json(new AjaxResult(AjaxResult.RESULT_OK, CSMSLang.Get("message_save_success")));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return Json(new AjaxResult(AjaxResult.RESULT_ERROR, ex.Message));
            }
        }

        #endregion

        #region 编辑/查看

        [AuthorizeFilter("Manage.GridColumn")]
        // GET: Manage/Role/Edit/5
        public ActionResult Edit(int id)
        {
            var service = new S_gridcolumnService();

            //资源分类
            ViewBag.PagekeyList = new SelectList(service.DistinctPagekey());
            //对齐方式
            Dictionary<string, string> alignlist = new Dictionary<string, string>();
            alignlist.Add("left", "left");
            alignlist.Add("center", "center");
            alignlist.Add("right", "right");

            if (id == 0)
            {
                ViewBag.AlignList = new SelectList(alignlist, "Key", "Value", "left");

                return View(new S_gridcolumn
                {
                    Width = "70",
                    Sortable = 1,
                    Islist = 1,
                    Isexport = 1
                });
            }
            else
            {
                var model = service.Find(id);

                ViewBag.AlignList = new SelectList(alignlist, "Key", "Value", model.Align);

                return View(model);
            }
        }

        #endregion

        #region GetNextSort

        [AuthorizeFilter("Manage.GridColumn")]
        public int GetNextSort(string pagekey)
        {
            return new S_gridcolumnService().GetNextSort(pagekey);
        }

        #endregion

    }
}